package com.ptu.spzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.exception.ptuException;
import com.ptu.spzx.domain.Course.Course;
import com.ptu.spzx.domain.Course.DTO.CourseDto;
import com.ptu.spzx.domain.Course.DTO.CourseSelectDto;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.feign.userInfo.ManagerFeignClient;
import com.ptu.spzx.mapper.CourseMapper;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.ICourseService;
import com.ptu.spzx.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ManagerFeignClient managerFeignClient;
    @Override
    public void addCourse(CourseDto courseDto) {
        Course course = BeanCopyUtils.copyBean(courseDto, Course.class);
        courseMapper.insert(course);
        List<Long> teacherId = courseDto.getTeacherId();
        managerFeignClient.addCourseUser(teacherId,course.getId());
    }

    @Override
    public void updateCourse(CourseDto courseDto) {
        Course course = BeanCopyUtils.copyBean(courseDto, Course.class);
        courseMapper.updateById(course);
        List<Long> teacherId = courseDto.getTeacherId();
        Long id = courseDto.getId();
        managerFeignClient.deleteCourseUser(id);
        managerFeignClient.addCourseUser(teacherId,course.getId());
    }

    @Override
    public void deleteCourse( DeleteIds deleteIds) {
        List<Long> ids = deleteIds.getIds();
        if(ids==null||ids.size()==0){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        courseMapper.deleteBatchIds(ids);
         for(Long courseId:ids){
             managerFeignClient.deleteCourseUser(courseId);
         }
    }

    @Override
    public PageInfo<Course> getCourseList(Long teachId,Integer pageNum, Integer pageSize) {
        if(teachId==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        List<Long> courseIds = managerFeignClient.selectCourseIdByteachId(teachId);
        if(courseIds.size()==0){
            throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<Course> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Course::getId,courseIds);
        PageHelper.startPage(pageNum,pageSize);
        List<Course> courses = courseMapper.selectList(lambdaQueryWrapper);
        if(courses==null){
            throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        PageInfo<Course> coursePageInfo=new PageInfo<>(courses);
        return coursePageInfo;
    }

    @Override
    public PageInfo<Course> findByPage(Integer pageNum, Integer pageSize, CourseSelectDto courseSelectDto) {
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper=new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.like(Course::getCourseName,courseSelectDto.getKeyword());
        PageHelper.startPage(pageNum,pageSize);
        List<Course> courses = courseMapper.selectList(courseLambdaQueryWrapper);
        PageInfo<Course> pageInfo=new PageInfo<>(courses);
        return pageInfo;
    }

    @Override
    public Course getCourseById(Long courseId) {
        if(courseId==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        Course course = courseMapper.selectById(courseId);
        if(course==null){
            throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return course;
    }


}
